package ouc.pkere.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import ouc.pkere.mapper.ExtractionMapper;
import ouc.pkere.mapper.RelationMapper;
import ouc.pkere.service.RunTasksService;
import ouc.pkere.service.VisualizationService;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class PkereController {
    @Autowired
    RunTasksService runTasksService;

    @Autowired
    VisualizationService visualizationService;

    @Autowired
    RelationMapper relationMapper;

    @RequestMapping(value = "/relation-extraction", method = {RequestMethod.POST, RequestMethod.GET})
    public String relationExtractionView () {
        return "relation-extraction";
    }

    @RequestMapping(value = "/view-graph", method = {RequestMethod.POST, RequestMethod.GET})
    public String viewGraphView () {
        return "view-graph";
    }

    //Content-Type: application/json;charset=UTF-8
    //{"tableName": "shijian,shiti", "columns": {"shijian":"huzhuxingming,bangfurenxingming","shiti":"xingming,renmingshibie"}, "primaryKey": {"shijian":"shijianid","shiti":"huzhushenfenzheng"}}
    @RequestMapping(value = "/extract", method = {RequestMethod.POST})
    @ResponseBody
    public Map<String, Object> runTask (@RequestBody Map<String, Object> requestMap) throws IOException {
        Map<String, Object> map = new HashMap<>();
        Map<String, String> col =  (Map)requestMap.get("columns");
        Map<String, String> primaryKey = (Map)requestMap.get("primaryKey");

        System.out.println(col.toString());
        System.out.println(primaryKey.toString());
        runTasksService.extractFromTable(String.valueOf(requestMap.get("tableName")).split(","), col,primaryKey);
        map.put("message", "success");
        return map;
    }


    @RequestMapping(value = "/visualize/get-value-randomly", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Map<String, Object> getAValueRandomly () {
        Map<String, Object> map = new HashMap<>();
        map.put("value", relationMapper.getAValueRandomly());
        return map;
    }

    //{"tableName": "shijian,shiti", "col": {"shijian":"huzhuxingming,bangfurenxingming","shiti":"xingming,renmingshibie"}, "primaryKey": {"shijian":"shijianid","shiti":"huzhushenfenzheng"}}
    @RequestMapping(value = "/visualize/all", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Map<String, Object> visualizeAll () {
        return visualizationService.getAllDataFromDatabase();
    }

    @RequestMapping(value = "/visualize/one", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Map<String, Object> visualizeOne (@RequestParam Map<String, String> requestMap) {
        return visualizationService.getDataByValueFromDatabase(requestMap.get("name"), Integer.valueOf(requestMap.get("depth")));
    }

    @RequestMapping(value = "/visualize/detail", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Map<String, Object> getDetail (@RequestParam Map<String, String> requestMap) throws Exception {
        return visualizationService.getDetail(String.valueOf(requestMap.get("value")));
    }

}
